package collector.app.nexd.com.singlecollection.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import collector.app.nexd.com.singlecollection.R;
import collector.app.nexd.com.singlecollection.model.CollectConfig;
import collector.app.nexd.com.singlecollection.presenter.setting.SettingContract;
import collector.app.nexd.com.singlecollection.presenter.setting.SettingPresenter;
import collector.app.nexd.com.singlecollection.util.AnimUtil;
import collector.app.nexd.com.singlecollection.util.DateUtil;

public class SettingActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener, SettingContract.View {
    private Toolbar tlBar;
    private static final String TAG = "SettingActivity";
    private Switch beaconSwitch, wifiSwitch;
    private View beaconDetail, wifiDetail;
    private float[] foldAngle = new float[2];
    private float[] unfoldAngle = new float[2];
    private Button btnSubmit;
    private EditText etWifiSpeed, etWifiTime, etBeaconSpeed, etBeaconTime;
    private SettingContract.Presenter settingPresenter;
    private List<CollectConfig> collectConfigs = new ArrayList<>();
    private String userName;
    private TextView tvUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        settingPresenter = new SettingPresenter(this);
        initData();
        initAnimParam();
        initView();
        bindData();
        bindListener();
    }

    private void initData() {
        userName = getIntent().getStringExtra(SignInActivity.EXTRA_NAME);
        if (userName == null || TextUtils.isEmpty(userName)) {
            userName = "test";
        }
    }

    private void bindData() {
        tvUserName.setText(userName);
    }

    private void bindListener() {
        beaconSwitch.setOnCheckedChangeListener(this);
        wifiSwitch.setOnCheckedChangeListener(this);
        btnSubmit.setOnClickListener(this);
    }

    private void initAnimParam() {
        foldAngle[0] = 90;
        foldAngle[1] = 0;
        unfoldAngle[0] = 0;
        unfoldAngle[1] = 90;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    public void initView() {
        tvUserName = (TextView) findViewById(R.id.setting_collect_tv_username);
        tlBar = (Toolbar) findViewById(R.id.setting_tl);
        btnSubmit = (Button) findViewById(R.id.setting_btn_submit);
        beaconSwitch = (Switch) findViewById(R.id.switch_beacon_setting);
        wifiSwitch = (Switch) findViewById(R.id.switch_wifi_setting);
        beaconDetail = findViewById(R.id.beacon_setting_detail);
        wifiDetail = findViewById(R.id.wifi_setting_detail);
        etWifiSpeed = (EditText) wifiDetail.findViewById(R.id.setting_collect_et_speed);
        etWifiTime = (EditText) wifiDetail.findViewById(R.id.setting_collect_et_time);
        etBeaconSpeed = (EditText) beaconDetail.findViewById(R.id.setting_collect_et_speed);
        etBeaconTime = (EditText) beaconDetail.findViewById(R.id.setting_collect_et_time);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(SettingActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startActivity() {
        startActivity(new Intent(this, SelectBuildingActivity.class));
        finish();
    }

    @Override
    public void printLog(String msg) {
        Log.d(TAG, "printLog: " + msg);
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.switch_beacon_setting:
                if (isChecked) {
                    AnimUtil.foldAnim(beaconDetail, foldAngle);
                } else {
                    AnimUtil.unFoldAnim(beaconDetail, unfoldAngle);
                }
                break;
            case R.id.switch_wifi_setting:
                if (isChecked) {
                    AnimUtil.foldAnim(wifiDetail, foldAngle);
                } else {
                    AnimUtil.unFoldAnim(wifiDetail, unfoldAngle);

                }
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_btn_submit:
                checkCollectWay();
                settingPresenter.submitMsg(collectConfigs);
                break;
        }
    }

    private void checkCollectWay() {
        String text = "";
        String textOther = "";
        String collectWayOther = "";
        String collectWay = "";
        String cacheTime = "";
        String cacheTimeOther = "";
        if (beaconSwitch.isChecked() && !wifiSwitch.isChecked()) {
            text = etBeaconSpeed.getText().toString();
            collectWay = beaconSwitch.getText().toString();
            cacheTime = etBeaconTime.getText().toString();
        }
        if (wifiSwitch.isChecked() && !beaconSwitch.isChecked()) {
            text = etWifiSpeed.getText().toString();
            collectWay = wifiSwitch.getText().toString();
            cacheTime = etWifiTime .getText().toString();
        }
        if (wifiSwitch.isChecked() && beaconSwitch.isChecked()) {
            text = etWifiSpeed.getText().toString();
            collectWay = wifiSwitch.getText().toString();
            textOther = etBeaconSpeed.getText().toString();
            collectWayOther = beaconSwitch.getText().toString();
            cacheTime = etWifiTime.getText().toString();
            cacheTimeOther = etBeaconTime.getText().toString();
        }
        if (!TextUtils.isEmpty(text)) {
            collectConfigs.add(new CollectConfig(collectWay, text, cacheTime));
            if (!TextUtils.isEmpty(textOther)) {
                collectConfigs.add(new CollectConfig(collectWayOther, textOther, cacheTimeOther));
            }
        }

    }


}
